﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO.IsolatedStorage;
using System.IO;
using System.Xml.Serialization;
using System.Xml.Linq;
using System.Net;
using System.Text;

namespace Simple_ToDo.Phone
{
    public class SaveAndSync
    {
        public static void Save(TaskControl t)
        {
            using(IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using(IsolatedStorageFileStream fs = storage.CreateFile("Tasks.xml"))
                {
                    using(StreamWriter writer = new StreamWriter(fs))
                    {
                        XmlSerializer serializer = new XmlSerializer(typeof(TaskControl));
                        serializer.Serialize(writer, t);
                    }
                }
            }
        }

        public static TaskControl Load()
        {
            TaskControl t = null;

            using(IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using(IsolatedStorageFileStream fs = storage.OpenFile("Tasks.xml", FileMode.Open))
                {
                    XDocument doc = XDocument.Load(fs);
                    XmlSerializer serializer = new XmlSerializer(typeof(TaskControl));
                    t = (TaskControl)serializer.Deserialize(doc.CreateReader());
                }
            }

            return t;
        }

        public static void UploadToFTP()
        {
            //FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://77.47.179.129/Upload/Tasks.xml");
            //request.Method = WebRequestMethods.Ftp.UploadFile;

            //request.Credentials = new NetworkCredential("anonymous", "anonymous");

            //TextReader textReader = new StreamReader("Tasks.xml");
            //byte[] fileContents = Encoding.UTF8.GetBytes(textReader.ReadToEnd());
            //textReader.Close();
            //request.ContentLength = fileContents.Length;

            //Stream requestStream = request.GetRequestStream();
            //requestStream.Write(fileContents, 0, fileContents.Length);
            //requestStream.Close();

            //FtpWebResponse response = (FtpWebResponse)request.GetResponse();
            //response.Close();
        }
    }
}
